package com.taptech.doufu.util;

import android.annotation.SuppressLint;

import java.sql.Timestamp;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class TimeDateFunction {

	/**
	 * 自定义时间转换器
	 *
	 * @param time
	 * @return
	 */
	@SuppressLint("SimpleDateFormat")
	public static String custTimeReplace(long time, String str_Time) {
		SimpleDateFormat sdf = new SimpleDateFormat(str_Time);
		Date longD = new Date(time);
		String resultD = sdf.format(longD);
		return resultD;
	}

	// 时间转换函数
	@SuppressLint("SimpleDateFormat")
	public static String toDateTime(long time) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date longD = new Date(time);
		String resultD = sdf.format(longD);
		return resultD;
	}

	public static String toDateTime_1(long time) {
		SimpleDateFormat sdf = new SimpleDateFormat("MM-dd HH:mm");
		Date longD = new Date(time);
		String resultD = sdf.format(longD);
		return resultD;
	}

	public static String toDateTime_2(long time) {
		SimpleDateFormat sdf = new SimpleDateFormat("MM月dd日");
		Date longD = new Date(time);
		String resultD = sdf.format(longD);
		return resultD;
	}

	public static String toYear(long time) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
		Date longD = new Date(time);
		String resultD = sdf.format(longD);
		return resultD;
	}

	public static int toTimeMM(long time)
	{
		SimpleDateFormat sdf = new SimpleDateFormat("HH");
		Date longD = new Date(time);
		String resultD = sdf.format(longD);
		return Integer.parseInt(resultD);
	}

	public static String getStrTime(String cc_time) {
		String re_StrTime = null;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		// 例如：cc_time=1291778220
		long lcc_time = Long.valueOf(cc_time);
		re_StrTime = sdf.format(new Date(lcc_time * 1000L));
		return re_StrTime;
	}

	// 时间转换函数(精确到分钟)
	@SuppressLint("SimpleDateFormat")
	public static String getDateTimeToMinute_Num(long time) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		Date longD = new Date(time);
		String resultD = sdf.format(longD);
		return resultD;
	}

	// 时间转换函数(将秒数精确到分钟)
	@SuppressLint("SimpleDateFormat")
	public static String getDateSecondToMinute_Num(long time) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		Date longD = new Date(time);
		String resultD = sdf.format(longD);
		return resultD;
	}

	// 时间转换函数(精确到分钟)
	@SuppressLint("SimpleDateFormat")
	public static String getDateTimeToMinute_Str(long time) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分");
		Date longD = new Date(time);
		String resultD = sdf.format(longD);
		return resultD;
	}

	public static String getDateTimeToY_M_DStr(long time) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
		Date longD = new Date(time);
		String resultD = sdf.format(longD);
		return resultD;
	}

	@SuppressLint("SimpleDateFormat")
	public static String toTime(long time) {
		SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
		Date longD = new Date(time);
		String resultD = sdf.format(longD);
		return resultD;
	}

	@SuppressLint("SimpleDateFormat")
	public static String toDate(long time) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date longD = new Date(time);
		String resultD = sdf.format(longD);
		return resultD;
	}

	@SuppressLint("SimpleDateFormat")
	public static String toDataDay(long time) {
		SimpleDateFormat sdf = new SimpleDateFormat("MM-dd");
		Date longD = new Date(time);
		String resultD = sdf.format(longD);
		return resultD;
	}

	public static String toTimeM(long time) {
		SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
		Date longD = new Date(time);
		String resultD = sdf.format(longD);
		return resultD;
	}

	// 判断是否是当天日期
	public static boolean isCurrtentDate(long time)
	{
		String date1 = toDate(time);
		String date2 = toDate(getDataTimeInMillis());
		if (date1.equals(date2)) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 获取当前时间
	 * @return
	 */
	public static long getCurrTime() {
		Calendar localCalendar = Calendar.getInstance();
		return localCalendar.getTimeInMillis();
	}

	/**
	 * 获取当前时间秒数
	 * @return
	 */
	public static int getCurrTimeInSec() {
		long time = getCurrTime();
		int sec = (int) (time / 1000);
		return sec;
	}

	public static int getTimeInsec(long time) {
		int sec = (int) (time / 1000);
		return sec;
	}

	/**
	 * 获取当天时间戳
	 */
	public static long getDataTimeInMillis() {
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.HOUR, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.MILLISECOND, 0);
		return cal.getTimeInMillis();
	}

	/**
	 * @author LuoB.
	 * @param oldTime 较小的时间
	 * @param newTime 较大的时间 (如果为空   默认当前时间 ,表示和当前时间相比)
	 * @return -1 ：同一天.    0：昨天 .   1 ：至少是前天.
	 * @throws ParseException 转换异常
	 */
	@SuppressLint("SimpleDateFormat")
	public static int isYeaterday(long oldTime,Date newTime){
		try
		{
			if(newTime==null){
				newTime=new Date();
			}
			//将下面的 理解成  yyyy-MM-dd 00：00：00 更好理解点
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
			String todayStr = format.format(newTime);
			Date today = format.parse(todayStr);
			//昨天 86400000=24*60*60*1000 一天
			if((today.getTime()-oldTime)>0 && (today.getTime()-oldTime)<=86400000) {
				return 0;
			}
			else if((today.getTime()-oldTime)<=0){ //至少是今天
				return -1;
			}
			else{ //至少是前天
				return 1;
			}
		}
		catch(Exception e)
		{

		}
		return -2;
	}

	/**
	 * 获取前N天的日期
	 */
	@SuppressLint("SimpleDateFormat")
	public static boolean isBeforeNDay(int n, long time) {
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.DATE, -n);
		String yesterday = new SimpleDateFormat("yyyy-MM-dd").format(cal
				.getTime());
		String currentday = toDate(time);
		if (yesterday.trim().equals(currentday.trim())) {
			return true;
		} else
			return false;
	}

	/**
	 * 判断二个时间是否在同一个周
	 *
	 * @param date1
	 * @param date2
	 * @return
	 */
	public static boolean isSameWeekDates(Date date1, Date date2) {
		Calendar cal1 = Calendar.getInstance();
		Calendar cal2 = Calendar.getInstance();
		cal1.setTime(date1);
		cal2.setTime(date2);
		int subYear = cal1.get(Calendar.YEAR) - cal2.get(Calendar.YEAR);
		if (0 == subYear) {
			if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2
					.get(Calendar.WEEK_OF_YEAR))
				return true;
		} else if (1 == subYear && 11 == cal2.get(Calendar.MONTH)) {
			// 如果12月的最后一周横跨来年第一周的话则最后一周即算做来年的第一周
			if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2
					.get(Calendar.WEEK_OF_YEAR))
				return true;
		} else if (-1 == subYear && 11 == cal1.get(Calendar.MONTH)) {
			if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2
					.get(Calendar.WEEK_OF_YEAR))
				return true;
		}
		return false;
	}

	/**
	 * 判断日期是周几
	 * **/
	public static String getWeekOfDate(Date dt) {
		String[] weekDays = { "周日", "周一", "周二", "周三", "周四", "周五", "周六" };
		Calendar cal = Calendar.getInstance();
		cal.setTime(dt);
		int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
		if (w < 0)
			w = 0;
		return weekDays[w];
	}

	public static int getWeekOfDateInteger(Date dt) {
		int[] weekDays = {7, 1, 2, 3, 4, 5,6 };
		Calendar cal = Calendar.getInstance();
		cal.setTime(dt);
		int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
		if (w < 0)
			w = 0;
		return weekDays[w];
	}

	// 获取date对象
	public static Date getDay(long time) {
		// SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date day = new Date(time);
		return day;
	}

	// 获取date对象
	public static Date getDay(String date) {
		// SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date day = new Date(Long.parseLong(getTime_1(date)));
		return day;
	}

	// 获取两个日期之间相隔几天
	public static long getDaysBetween(Date date0, Date date1) {
		long daysBetween = (date0.getTime() - date1.getTime() + 1000000) / 86400000;// 86400000=3600*24*1000
		// 用立即数，减少乘法计算的开销
		return daysBetween;
	}

	// 将字符串转为时间戳
	@SuppressLint("SimpleDateFormat")
	public static String getTime(String user_time) {
		String re_time = null;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date d;
		try {
			d = sdf.parse(user_time);
			long l = d.getTime();
			String str = String.valueOf(l);
			re_time = str;
		} catch (ParseException e) {
		}
		return re_time;
	}

	// 将字符串转为时间戳
	@SuppressLint("SimpleDateFormat")
	public static String getTime_2(String user_time) {
		String re_time = null;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		Date d;
		try {
			d = sdf.parse(user_time);
			long l = d.getTime();
			String str = String.valueOf(l);
			re_time = str;
		} catch (ParseException e) {
		}
		return re_time;
	}

	// 将字符串转为时间戳
	@SuppressLint("SimpleDateFormat")
	public static String getTime_1(String user_time) {
		String re_time = null;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date d;
		try {
			d = sdf.parse(user_time);
			long l = d.getTime();
			String str = String.valueOf(l);
			re_time = str;
		} catch (ParseException e) {
		}
		return re_time;
	}


	// 将字符串转为时间戳
	@SuppressLint("SimpleDateFormat")
	public static String getTime_3(String user_time) {
		String re_time = null;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
		Date d;
		try {
			d = sdf.parse(user_time);
			long l = d.getTime();
			String str = String.valueOf(l);
			re_time = str;
		} catch (ParseException e) {
		}
		return re_time;
	}

	/**
	 * 将字符串转为时间戳 long
	 *
	 * @param user_time
	 * @return
	 */
	public static long getTimeToLong(String user_time) {

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date d;
		try {
			d = sdf.parse(user_time);
			return d.getTime() / 1000;

		} catch (ParseException e) {
		}
		return 0;
	}

	public static long getTimeStamp(String time){
		SimpleDateFormat simpleDateFormat =new SimpleDateFormat("yyyy-MM-dd ");
		Date date= null;
		try {
			date = simpleDateFormat .parse(time);
			long timeStemp = date.getTime();
			return  timeStemp;
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return 0;

	}

	/**
	 * 将日期转为时间戳 long
	 *
	 * @param user_time
	 * @return
	 */
	public static long getTimeToLongNew(String user_time) {

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date d;
		try {
			d = sdf.parse(user_time);
			return d.getTime() ;

		} catch (ParseException e) {
		}
		return 0;
	}

	/**
	 * 将时间转换为合适的格式显示
	 * @param time
	 * @return
	 */
	public static String transferTime(long time)
	{

		long time2 = TimeDateFunction.getCurrTime();
		String sData1 = TimeDateFunction.toDate(time);
		String sData2 = TimeDateFunction.toDate(time2);
		// 设置时间
		if (sData1.equals(sData2)) {
			return TimeDateFunction.toTimeM(time);
		} else if (TimeDateFunction.custTimeReplace(time, "yyyy").equals(
				TimeDateFunction.custTimeReplace(time2, "yyyy"))) {
			return TimeDateFunction.custTimeReplace(time,
					"MM-dd HH:mm");
		} else {
			return TimeDateFunction.custTimeReplace(time,
					"yyyy-MM-dd");
		}
	}



	public static int formatRecordTime(long recordTime){
		float durf= Float.parseFloat(String.valueOf(recordTime))/1000f;
		DecimalFormat df=new DecimalFormat("#.#");
		return (int) Math.ceil(Double.parseDouble(df.format(durf)));
	}

	public static String getRecordTime(int sec) {
		int hours, minute, second;
		int temp = sec / 60;
		second = sec % 60;
		hours = temp / 60;
		minute = temp % 60;
		StringBuilder builder = new StringBuilder();
		if (hours > 0) {
			builder.append(hours).append("时");
		}
		if (minute > 0 || (hours > 0 && minute == 0)) {
			builder.append(minute).append("分");
		}
		builder.append(second).append("秒");
		return builder.toString();
	}

	public static String getRecordTimeStatis(int sec) {
		int hours, minute, second;
		int temp = sec / 60;
		second = sec % 60;
		hours = temp / 60;
		minute = temp % 60;
		StringBuilder builder = new StringBuilder();
		if (hours > 0) {
			builder.append(hours).append("时");
		}
		builder.append(minute).append("分");
		builder.append(second).append("秒");
		return builder.toString();
	}

	public static String getTime(int timestamp){
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String time= null;
		try {
			java.util.Date currentdate = new java.util.Date();//当前时间
			long i = (currentdate.getTime()/1000-timestamp)/(60);
//	  			 Timestamp now = new Timestamp(System.currentTimeMillis());//获取系统当前时间
//	  			 System.out.println("now-->"+now);//返回结果精确到毫秒。
			String str = sdf.format(new Timestamp(IntToLong(timestamp)));
			return str;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return time;
	}

	public static String getTimeToYMD(long timestamp){
		SimpleDateFormat sdf=new SimpleDateFormat("/yyyy/MM/dd/");
		String time= null;
		try {
			time = sdf.format(new Date(IntToLong(1407807033)));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return time;
	}

	/**
	 * 获取将来的日期的时间戳
	 * @param milliseconds  当前时间
	 * @param count  与当前时间相隔的天数
	 * @return
	 */
	public static long getFutureTime(long milliseconds,int count)
	{
		int[] times = getTomorrowTime(milliseconds, count);
		String ss = toTime(getCurrTime());
		StringBuilder date = new StringBuilder();
		if(times.length>2)
		{
			date.append(times[0]).append("-").append(times[1]).append("-").append(times[2]);
			date.append(" ").append(ss);
			return Long.parseLong(TimeDateFunction.getTime(date.toString()));
		}
		return 0;
	}

	public static int[] getTomorrowTime(long milliseconds,int count)
	{
		Calendar cal= Calendar.getInstance();
		cal.setTimeInMillis(milliseconds);
		cal.add(Calendar.DAY_OF_YEAR, +count);
		int year = cal.get(Calendar.YEAR);
		int month = cal.get(Calendar.MONTH)+1;
		int day = cal.get(Calendar.DATE);
		return new int[]{year,month,day};
	}

	//java Timestamp构造函数需传入Long型
	public static long IntToLong(int i){
		long result = (long)i;
		result*=1000;
		return  result;
	}

	public static String getDate(String month, String day){
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//24小时制
		java.util.Date d = new java.util.Date(); ;
		String str = sdf.format(d);
		String nowmonth = str.substring(5, 7);
		String nowday = str.substring(8,10 );
		String result = null;

		int temp =   Integer.parseInt(nowday)- Integer.parseInt(day);
		switch (temp) {
			case 0:
				result="今天";
				break;
			case 1:
				result = "昨天";
				break;
			case 2:
				result = "前天";
				break;
			default:
				StringBuilder sb = new StringBuilder();
				sb.append(Integer.parseInt(month)+"月");
				sb.append(Integer.parseInt(day)+"日");
				result  = sb.toString();
				break;
		}
		return result;
	}

	/**
	 *
	 * @param date 2015-12-12
	 * @return 2015年12月12日
	 */
	public static String fomateDate(String date)
	{
		StringBuilder dateStr = new StringBuilder();
		String[] element = date.split("-");
		if (element.length>2)
		{
			dateStr.append(element[0]).append("年");
			dateStr.append(element[1]).append("月");
			dateStr.append(element[2]).append("日");
		}
		return dateStr.toString();
	}

}
